package com.fymxy.question_bank_management.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fymxy.question_bank_management.pojo.Problem;
import com.fymxy.question_bank_management.pojo.ProblemWithBLOBs;
import java.util.List;

import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

@Mapper
@Component
public interface ProblemMapper {


    int deleteByPrimaryKey(Integer problemNo);

    @Insert("insert into problem (problem_no,problem_content,problem_level,problem_answer_content,problem_type,problem_setter_no) values" +
            "(#{problemNo},#{problemContent},#{problemLevel},#{problemAnswerContent},#{problemType},#{problemSetterNo})")
    int insert(ProblemWithBLOBs record);

    int insertSelective(ProblemWithBLOBs record);

    @Select("select * from problem")
    @Results(id = "ProblemMap",value = {
            @Result(column = "problem_no",property = "problemNo"),
            @Result(column = "problem_content",property = "problemContent" ),
            @Result(column = "problem_level",property = "problemLevel"),
            @Result(column = "problem_answer_content",property = "problemAnswerContent"),
            @Result(column = "problem_type",property = "problemType"),
            @Result(column = "problem_setter_no",property = "problemSetterNo"),
    })
    IPage<ProblemWithBLOBs> selectAllProblem(Page page);

    @Select("select * from problem where problem_no = #{problemNo}")
    @ResultMap(value = "ProblemMap")
    Problem selectByProblemNo(Integer problemNo);

    @Select("select problem.problem_no,problem.problem_answer_content,problem.problem_content,problem.problem_level,problem.problem_setter_no,problem.problem_type" +
            " from problem where problem_no = #{problemNo}")
    @ResultMap(value = "ProblemMap")
    ProblemWithBLOBs selectAllByProblemNo(Integer problemNo);

    //由关键字，难度，题目类型以及知识点查找题目
    @Select("SELECT distinct problem.problem_no,problem.problem_answer_content,problem.problem_content,problem.problem_level,problem.problem_setter_no,problem.problem_type " +
            "from problem,keyword where (problem_answer_content like #{keyword} or problem_content like #{keyword}) and keyword_content like #{point} and problem_level like #{level} " +
            "and problem_type like #{type}")
    @ResultMap(value = "ProblemMap")
    IPage<ProblemWithBLOBs> selectByKeyword(Page page,String keyword,String level,String type,String point);

    @Select("select * from problem where problem_no = #{id}")
    @ResultMap(value = "ProblemMap")
    ProblemWithBLOBs selectById(Integer id);

    @Delete("delete from problem where problem_no = #{id}")
    int deleteProblemById(Integer id);
    ProblemWithBLOBs selectByPrimaryKey(Integer problemNo);

    //通过试卷号查找题目
    @Select("select DISTINCT problem.problem_no,problem.problem_answer_content,problem.problem_content,problem.problem_level,problem.problem_setter_no,problem.problem_type " +
            "from problem,paper2pro where problem.problem_no = paper2pro.problem_no and paper2pro.paper_no = #{paperNo} order by problem_type")
    @ResultMap(value = "ProblemMap")
    List<Problem> selectProblemByPaper(Integer paperNo);

    int updateByPrimaryKeySelective(ProblemWithBLOBs record);

    @Update("update problem set problem_content = #{problemContent},problem_answer_content = #{problemAnswerContent where problem_no = #{problemNo}")
    int updateProblemById(ProblemWithBLOBs record);


    int updateByPrimaryKey(Problem record);

    //查找最大试题编号(即刚插入的试题)
    @Select("select MAX(problem_no) from problem")
    int getTopProblemNo();

    //根据难度,知识点查询试题
    //keyword.problem_no = problem.problem_no
    @Select("<script>" +
            "select distinct problem.problem_no " +
            "from problem,keyword where keyword.problem_no = problem.problem_no " +
            "and problem_level = #{level} and problem_type=#{type} and keyword_content in" +
            "<foreach collection='keywords' open='(' item='keyword' separator=',' close=')'>#{keyword}</foreach > " +
            "</script>")
    //@ResultMap(value = "ProblemMap")
    List<Integer> selectByOption(@Param("keywords") List<String> keywords,@Param("level") String level,@Param("type") String type);

    @ResultMap(value = "ProblemMap")
    @Select("select DISTINCT * from problem ,paper2pro where problem.problem_no = paper2pro.problem_no and paper_no = #{paperId}.")
    List<ProblemWithBLOBs> selectByPaper(Integer paperId);
}